package com.ostrich.note.ui.note.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ostrich.note.R;
import com.ostrich.note.databinding.ItemNoteBinding;
import com.ostrich.note.databinding.ItemNoteTypeBinding;
import com.ostrich.note.ui.note.entity.NoteEntity;
import com.ostrich.note.ui.note.entity.TypeEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;


public class NoteTypeAdapter extends RecyclerView.Adapter<NoteTypeAdapter.NoteTypeViewHolder> {
    private List<TypeEntity.ListBean> mList = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    //数据做刷新
    public void setData(List<TypeEntity.ListBean> list){
        if(list != null && list.size()>0){
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public NoteTypeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNoteTypeBinding itemNoteTypeBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_note_type, parent, false);
        return new NoteTypeViewHolder(itemNoteTypeBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteTypeViewHolder holder, int position) {
        holder.mItemNoteTypeBinding.setNoteType(mList.get(position));


        holder.mItemNoteTypeBinding.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class NoteTypeViewHolder extends RecyclerView.ViewHolder{
        private ItemNoteTypeBinding mItemNoteTypeBinding;
        public NoteTypeViewHolder(ItemNoteTypeBinding mItemNoteTypeBinding) {
            super(mItemNoteTypeBinding.getRoot());
            this.mItemNoteTypeBinding = mItemNoteTypeBinding;


        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }


}
